package com.ibm.csorb.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.csorb.dao.CsCycleTcDao;
import com.ibm.csorb.entity.CsCycleTc;

@Component
@Transactional
public class CsCycleTcDaoImpl extends HibernateDaoSupport implements
		CsCycleTcDao {
	private static final Log log = LogFactory.getLog(CsCycleTcDaoImpl.class);
	// property constants
	public static final String CYCLE_BT_ID = "cycleBtId";
	public static final String TC_ID = "tcId";
	public static final String STATUS = "status";

	protected void initDao() {
		// do nothing
	}

	public void save(CsCycleTc transientInstance) {
		log.debug("saving CsCycleTc instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CsCycleTc persistentInstance) {
		log.debug("deleting CsCycleTc instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CsCycleTc findById(java.lang.Integer id) {
		log.debug("getting CsCycleTc instance with id: " + id);
		try {
			CsCycleTc instance = (CsCycleTc) getHibernateTemplate().get(
					"com.ibm.csorb.entity.CsCycleTc", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsCycleTc> findByExample(CsCycleTc instance) {
		log.debug("finding CsCycleTc instance by example");
		try {
			List<CsCycleTc> results = getHibernateTemplate().findByExample(
					instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsCycleTc> findByProperty(String propertyName, Object value) {
		log.debug("finding CsCycleTc instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CsCycleTc as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<CsCycleTc> findByCycleBtId(Object cycleBtId) {
		return findByProperty(CYCLE_BT_ID, cycleBtId);
	}

	public List<CsCycleTc> findByTcId(Object tcId) {
		return findByProperty(TC_ID, tcId);
	}

	public List<CsCycleTc> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	@SuppressWarnings("unchecked")
	public List<CsCycleTc> findAll() {
		log.debug("finding all CsCycleTc instances");
		try {
			String queryString = "from CsCycleTc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CsCycleTc merge(CsCycleTc detachedInstance) {
		log.debug("merging CsCycleTc instance");
		try {
			CsCycleTc result = (CsCycleTc) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CsCycleTc instance) {
		log.debug("attaching dirty CsCycleTc instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CsCycleTc instance) {
		log.debug("attaching clean CsCycleTc instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CsCycleTcDaoImpl getFromApplicationContext(
			ApplicationContext ctx) {
		return (CsCycleTcDaoImpl) ctx.getBean("CsCycleTcDAO");
	}
}